import { AlertController, ModalController } from '@ionic/angular';
import { Component, Input, OnInit } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
import { PageModalService } from 'src/app/shared/service/page-modal-service';
import { EventService } from 'src/app/shared/service/event.service';
@Component({
  selector: 'app-dynamic-details-foot',
  templateUrl: './dynamic-details-foot.component.html',
  styleUrls: ['./dynamic-details-foot.component.scss'],
})
export class DynamicDetailsFootComponent implements OnInit {
  @Input() tagData: any = {};
  sendLabelValue: any;
  noShowFootBtn = true;
  timer: any;
  data: any;
  constructor(
    private pageModalService: PageModalService,
    private appGlobalService: AppGlobalService,
    public alertController: AlertController,
    private httpService: HttpService,
    public modalController: ModalController,
    public eventService: EventService,
  ) { }

  ngOnInit() {
    this.data = this.appGlobalService.dynamicDetailsData;
  }

  ionFocus() {
    const dom: any = document.querySelector('.recomment-bg2');
    dom.style.marginTop = '1px';
  }

  ionBlur() {
    const dom: any = document.querySelector('.recomment-bg2');
    dom.style.marginTop = '0px';
  }

  ionChange(e) {
    e.stopPropagation();
    if (this.noShowFootBtn !== false && this.sendLabelValue !== '') {
      setTimeout(() => {
        this.noShowFootBtn = false;
      }, 200);
    }

    clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      this.sendLabelValue = e.detail.value;
      if (this.sendLabelValue === '') {
        setTimeout(() => {
          this.noShowFootBtn = true;
        }, 100);
      }
    }, 100);
  }

  async sendLabel(e, item) {
    e.stopPropagation();
    const that = this;
    if (item && item.submit && item.submit.content.label) {
      const itemContent = JSON.parse(JSON.stringify(item.submit.content));
      itemContent[itemContent.label] = this.sendLabelValue;
      const params = {
        content: itemContent,
        id: item.submit.id,
        sessionId: localStorage.getItem("sessionId")
      };
      await this.httpService.post("application/submit", params, res => {
        if (res.success) {
          if (!that.data || that.data === undefined) {
            this.appGlobalService.reloadCurrentPage(800);
          } else {
            this.sendLabelValue = '';
            if (this.noShowFootBtn === false) {
              this.noShowFootBtn = true;
            }
            if (res.data && res.data.eventResult) {
              const dataItem = JSON.parse(JSON.stringify(that.data));
              const arr = res.data.eventResult.split("##$$");
              const id = arr[5];
              dataItem.firstRowLeft.firstText.label = arr[1];
              dataItem.firstRowLeft.firstText.img = arr[2];
              dataItem.firstRowLeft.secondText.value = arr[3];
              dataItem.firstRowLeft.secondText.label = arr[4];
              dataItem.firstRowLeft.secondText.subSecondDataSlice = [] ;
              dataItem.firstRowRight.firstText.submit.content.user_evaluate_circle_id = id;
              dataItem.firstRowRight.secondText.submit.content.user_evaluate_circle_id = id;
              dataItem.firstRowRight.secondText
                .icon = 'https://zeno-img.oss-cn-shenzhen.aliyuncs.com/file_1683356627189868ce62f30825acfde3a6b9224a96f8e.png';
              this.eventService.eventEmit.emit("dynamicDetails_dataItem", dataItem);
              // this.tagData.data.data.push(dataItem);
              // 评论数 +1
              this.tagData.data.footDiv.firstRowRight.secondText
                .label = Number(this.tagData.data.footDiv.firstRowRight.secondText.label) + 1;
            }
          }
        }
        this.appGlobalService.showToast(res.message, 1500, 'middle');
      });
    }
  }

}
